package com.example.flshtarvel.controller;

import com.example.flshtarvel.entity.PageResult;
import com.example.flshtarvel.entity.User;
import com.example.flshtarvel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class UserController {

    @Autowired
    UserService userService;


    @GetMapping("/user/{userId}")
    public Map<String,Object> getUserById(@PathVariable("userId") String userId){
        Map<String, Object> map = new HashMap<String, Object>();
        User user = userService.getUserById(userId);
        if(user==null){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("user",user);
        }
        return map;
    }

    @GetMapping("user")
    public Map<String,Object> getAllUser(
                                         @RequestParam(name="page",defaultValue="1") int page,
                                         @RequestParam(name="size",defaultValue = "10") int size){
        Map<String, Object> map = new HashMap<String, Object>();
        PageResult pageResult = new PageResult();
        List<User> userList = userService.getAllUser(page,size);

        if(userList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("userList",userList);
            int count = userService.selectUserCount();
            pageResult.setCount(count);
            pageResult.setCurrentPage(page);//当前页
            pageResult.setPageCount(count,size);
            pageResult.setPageSize(size);
            map.put("pageResult",pageResult);
        }
        return map;
    }

    @PostMapping("/user")
    public Map<String,Object> insertUser(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        int tag = userService.insertUser(user);
        if(tag==1){
            map.put("msg",1);
            map.put("user",user);
        }else if(tag==2){
            map.put("msg",2);
            map.put("message","已有该用户");
        }else{
            map.put("msg",0);
        }
        return map;
    }

    @PostMapping("/register")
    public Map<String,Object> register(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        int tag = userService.register(user);
        if(tag==1){
            map.put("msg",1);
            map.put("user",user);
        }else{
            map.put("msg",0);
        }
        return map;
    }

    @DeleteMapping("/user/{userId}")
    public Map<String,Object> deleteUser(@PathVariable("userId") String userId){
        Map<String,Object> map = new HashMap<String,Object>();
        int tag = userService.deleteUser(userId);

        map.put("msg",tag);
        return map;
    }

    @PostMapping("/updateUser")
    public Map<String,Object> updateUser(User user){
        Map<String,Object> map = new HashMap<String,Object>();
        int tag = userService.updateUser(user);
        if(tag==1){
            map.put("msg",1);
            map.put("user",user);
        }else{
            map.put("msg",0);
        }
        return map;
    }
}
